﻿using System;
using YAPL.Language.Compiler.CodeGen;

namespace YAPL.Language.Compiler {
	/// <summary>
	/// Main compiler class. Creates code generator, that produces output from given ast
	/// </summary>
	public class AstCompiler {
		private readonly OutputInfo _outputInfo;
		private readonly Ast.Expression _ast;

		public AstCompiler(OutputInfo outputInfo, Ast.Expression ast) {
			_outputInfo = outputInfo;
			_ast = ast;
		}

		public void GenerateOutput() {
			try {
				var generator = CodeGeneratorFactory.CreateCodeGenerator(_outputInfo);
				generator.GenerateCode(_ast);
			}
			catch (Exception e) {
				throw new CompilerException("Error compiling code",e);
			}
		}
	}
}
